package com.lxw.nonghe.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lxw.nonghe.annotation.RequiresPermission;
import com.lxw.nonghe.common.Result;
import com.lxw.nonghe.entity.FarmerPaymentDetail;
import com.lxw.nonghe.service.FarmerPaymentDetailService;
import com.lxw.nonghe.util.LogUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.Date;

/**
 * <p>
 * 参合人员缴费明细表 前端控制器
 * </p>
 *
 * @author lxw
 * @since 2025-05-14
 */
@RestController
@RequestMapping("/farmerPaymentDetail")
@RequiresPermission("paymentDetail:manage")
public class FarmerPaymentDetailController {

    @Autowired
    private FarmerPaymentDetailService paymentDetailService;

    @Autowired
    private LogUtils logUtils;

    @PostMapping("/create")
    public Result<Boolean> addPaymentDetail(@RequestBody FarmerPaymentDetail paymentDetail) {
        try {
            // 初始化状态和时间
            if (paymentDetail.getStatus() == null) {
                paymentDetail.setStatus((byte) 0); // 默认未缴费
            }
            if (paymentDetail.getPaymentTime() == null && paymentDetail.getStatus() == 1) {
                paymentDetail.setPaymentTime(new Date()); // 已缴费状态需要设置缴费时间
            }

            boolean result = paymentDetailService.save(paymentDetail);
            if (result) {
                logUtils.recordLog("农民缴费明细管理", "新增缴费记录", "成功",
                        "农民ID:" + paymentDetail.getFarmerId() + ", 金额:" + paymentDetail.getAmount());
                return Result.success("农民缴费记录新增成功", true);
            } else {
                logUtils.recordLog("农民缴费明细管理", "新增缴费记录", "失败", "保存数据库失败");
                return Result.error("农民缴费记录新增失败");
            }
        } catch (Exception e) {
            logUtils.recordLog("农民缴费明细管理", "新增缴费记录", "失败", e.getMessage());
            return Result.error("系统异常: " + e.getMessage());
        }
    }

    @PostMapping("/delete/{id}")
    public Result<Boolean> deletePaymentDetail(@PathVariable Integer id) {
        try {
            FarmerPaymentDetail paymentDetail = paymentDetailService.getById(id);
            boolean result = paymentDetailService.removeById(id);

            if (result) {
                logUtils.recordLog("农民缴费明细管理", "删除缴费记录", "成功",
                        "记录ID:" + id + ", 农民ID:" + (paymentDetail != null ? paymentDetail.getFarmerId() : "未知"));
                return Result.success("农民缴费记录删除成功", true);
            } else {
                logUtils.recordLog("农民缴费明细管理", "删除缴费记录", "失败", "记录ID:" + id + "不存在");
                return Result.error("农民缴费记录删除失败");
            }
        } catch (Exception e) {
            logUtils.recordLog("农民缴费明细管理", "删除缴费记录", "失败", e.getMessage());
            return Result.error("系统异常: " + e.getMessage());
        }
    }

    @PostMapping("/update")
    public Result<Boolean> updatePaymentDetail(@RequestBody FarmerPaymentDetail paymentDetail) {
        try {
            // 如果状态改为已缴费且缴费时间为空，则设置当前时间
            if (paymentDetail.getStatus() != null && paymentDetail.getStatus() == 1
                    && paymentDetail.getPaymentTime() == null) {
                paymentDetail.setPaymentTime(new Date());
            }

            boolean result = paymentDetailService.updateById(paymentDetail);
            if (result) {
                logUtils.recordLog("农民缴费明细管理", "更新缴费记录", "成功",
                        "记录ID:" + paymentDetail.getDetailId());
                return Result.success("农民缴费记录更新成功", true);
            } else {
                logUtils.recordLog("农民缴费明细管理", "更新缴费记录", "失败",
                        "记录ID:" + paymentDetail.getDetailId() + "不存在");
                return Result.error("农民缴费记录更新失败");
            }
        } catch (Exception e) {
            logUtils.recordLog("农民缴费明细管理", "更新缴费记录", "失败", e.getMessage());
            return Result.error("系统异常: " + e.getMessage());
        }
    }

    @PostMapping("/pay/{id}")
    public Result<Boolean> pay(@PathVariable Integer id) {
        try {
            FarmerPaymentDetail paymentDetail = new FarmerPaymentDetail();
            paymentDetail.setDetailId(id);
            paymentDetail.setStatus((byte) 1); // 已缴费
            paymentDetail.setPaymentTime(new Date());

            boolean result = paymentDetailService.updateById(paymentDetail);
            if (result) {
                logUtils.recordLog("农民缴费明细管理", "标记为已缴费", "成功",
                        "记录ID:" + id);
                return Result.success("缴费状态更新成功", true);
            } else {
                logUtils.recordLog("农民缴费明细管理", "标记为已缴费", "失败", "记录ID:" + id + "不存在");
                return Result.error("缴费状态更新失败");
            }
        } catch (Exception e) {
            logUtils.recordLog("农民缴费明细管理", "标记为已缴费", "失败", e.getMessage());
            return Result.error("系统异常: " + e.getMessage());
        }
    }

    @GetMapping("/get/{id}")
    public Result<FarmerPaymentDetail> getPaymentDetailById(@PathVariable Integer id) {
        try {
            FarmerPaymentDetail paymentDetail = paymentDetailService.getById(id);
            if (paymentDetail != null) {
                return Result.success("农民缴费记录查询成功", paymentDetail);
            } else {
                logUtils.recordLog("农民缴费明细管理", "查询缴费详情", "失败", "记录ID:" + id + "不存在");
                return Result.error("未找到该农民缴费记录");
            }
        } catch (Exception e) {
            logUtils.recordLog("农民缴费明细管理", "查询缴费详情", "失败", e.getMessage());
            return Result.error("查询异常: " + e.getMessage());
        }
    }

    @GetMapping("/list")
    public Result<Page<FarmerPaymentDetail>> getPaymentDetailPage(
            @RequestParam(defaultValue = "1") int current,
            @RequestParam(defaultValue = "10") int size,
            @RequestParam(required = false) String farmerName,
            @RequestParam(required = false) String idCard,
            @RequestParam(required = false) Byte status) {
        try {
            Page<FarmerPaymentDetail> page = paymentDetailService.getPaymentDetailPage(
                    current, size, farmerName, idCard, status);
            return Result.success("农民缴费记录分页查询成功", page);
        } catch (Exception e) {
            logUtils.recordLog("农民缴费明细管理", "分页查询缴费记录", "失败", e.getMessage());
            return Result.error("分页查询异常: " + e.getMessage());
        }
    }
    // 条件查询方法
    @PostMapping("/query")
    public Result<Page<FarmerPaymentDetail>> queryPaymentDetails(
            @RequestBody FarmerPaymentDetail queryParam,
            @RequestParam(defaultValue = "1") int current,
            @RequestParam(defaultValue = "10") int size) {
        try {
            Page<FarmerPaymentDetail> page = paymentDetailService.queryPaymentDetails(
                    queryParam, current, size);
            return Result.success("农民缴费记录条件查询成功", page);
        } catch (Exception e) {
            logUtils.recordLog("农民缴费明细管理", "条件查询缴费记录", "失败", e.getMessage());
            return Result.error("条件查询异常: " + e.getMessage());
        }
    }
}
